<?php
/**
 * 物流费用（基础）
 * (model)
 */

namespace App\Models\Basics;

use Illuminate\Database\Eloquent\Model;
class Transport extends Model
{
    protected $table = 'b_transport';

    protected $guarded = [];

    public static $_transportFee = [
        1 => 'sea_fee',
        2 => 'air_fee',
    ];

    public static $_transportDay = [
        1 => 'sea_day',
        2 => 'air_day',
    ];

    public static $_transport = [
        1 => '海运',
        2 => '空运',
    ];

    public function area(){
        return $this->belongsTo(SysArea::class,'area_id');
    }

    public static function getTransport($areaId){
        return self::query()->where('area_id',$areaId)->first();
    }

    public static function getTransportFee($areaId){
        return self::query()->where('area_id',$areaId)->select('sea_fee','air_fee')->first();
    }

    public static function getTransportDay($areaId){
        return self::query()->where('area_id',$areaId)->select('sea_day','air_day')->first();
    }

    public static function getShipmentFee($areaId,$transport){
        if(!array_key_exists($transport,self::$_transportFee))return 0;
        $filed = self::$_transportFee[$transport];
        $value = self::query()->where('area_id',$areaId)->value($filed);
        return $value ?: 0;
    }

    /**
     * 获取运输时间（天数）
     * @param $areaId
     * @param $transport
     * @return int|mixed
     */
    public static function getShipmentDay($areaId,$transport){
        if(!array_key_exists($transport,self::$_transportDay))return 0;
        $filed = self::$_transportDay[$transport];
        $value = self::query()->where('area_id',$areaId)->value($filed);
        return $value ?: 0;
    }
}
